﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;
using HPIT.RentHouse.Common;

namespace HPIT.RentHouse.Service
{
    public class PermissionsService : IServicePermissions
    {
        /// <summary>
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<PerissionDTO> GetPageList(int start, int length, string name, ref int count)
        {
            var db = new RentEntity();
            var bs = new BaseService<T_Permissions>(db);
            var query = PredicateExtensions.True<T_Permissions>();
            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.And(e => e.Name.Contains(name));
            }
            var list = bs.GetPagedList(start, length, ref count, query, a => a.Id);
            var result = list.Select(a => new PerissionDTO
            {
                Id = a.Id,
                Name = a.Name,
                Description = a.Description
            }).ToList();
            return result;
        }
        /// <summary>
        /// 获取权限id和权限名称
        /// </summary>
        /// <returns></returns>
        public List<PerissionDTO> GetList()
        {
            var db = new RentEntity();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var list = bs.GetList(r => true).Select(a => new PerissionDTO
            {
                Id=a.Id,    
                Description = a.Description 
            }).ToList();
            return list;
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="perissionDTO"></param>
        /// <returns></returns>
        public AjaxResult Add(PerissionDTO perissionDTO)
        {
            var db = new RentEntity();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            //实例化类对象
            T_Permissions per = new T_Permissions();
            per.Name = perissionDTO.Name;
            per.Description = perissionDTO.Description;
            per.CreateDateTime = DateTime.Now;
            //调用添加方法
            long id = bs.Add(per);
            if (id > 0)
            {
                return new AjaxResult(ResultState.Success, "权限添加成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限添加失败！");
            }
        }
        /// 根据id查询该条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PerissionDTO Get(long id)
        {
            var db = new RentEntity();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            //根据id查询该条数据是否存在
            T_Permissions model = bs.Get(a => a.Id == id);
            PerissionDTO dto = new PerissionDTO();
            if (model != null)
            {
                dto.Id = model.Id;
                dto.Name = model.Name;
                dto.Description = model.Description;
            }
            return dto;
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="perissionDTO"></param>
        /// <returns></returns>
        public AjaxResult Update(PerissionDTO perissionDTO)
        {
            var db = new RentEntity();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            //根据id查询该条数据
            var model = bs.Get(a => a.Id == perissionDTO.Id);
            model.Name = perissionDTO.Name;
            model.Description = perissionDTO.Description;
            bool res = bs.Update(model);
            if (res)
            {
                return new AjaxResult(ResultState.Success, "权限编辑成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限编辑失败！");
            }
        }
        /// <summary>
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult Delete(long id)
        {
            var db = new RentEntity();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            //根据id查询该条数据是否存在
            var model = bs.Get(a => a.Id == id);
            if (bs.Delete(model))
            {
                return new AjaxResult(ResultState.Success, "删除权限成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "删除权限失败！");
            }
        }
        /// <summary>
        /// 批量删除权限
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            try
            {
                var db = new RentEntity();
                BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
                //通过循环获取用户选中的多个id,并查询出数据，实现批量删除
                foreach (var id in ids)
                {
                    var model = bs.Get(a => a.Id == id);
                    bs.Delete(model);
                }
                return new AjaxResult(ResultState.Success, "批量删除成功！");
            }
            catch (Exception)
            {
                return new AjaxResult(ResultState.Error, "批量删除失败！");
            }
        }
        /// <summary>
        /// 获取当前登录用户的权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<PerissionDTO> GetListByUser(long userId)
        {
            var db=new RentEntity();
            var bs = new BaseService<T_AdminUsers>(db);
            //根据id查询当前登录用户的数据信息
            var user = bs.Get(a => a.Id == userId);
            //查询当前登录用户所拥有的权限信息
            List<T_Roles> roleList = user.T_Roles.ToList();
            //实例化类对象  通过foreach循环得到用户当前的权限
            List<PerissionDTO> perissionList = new List<PerissionDTO>();
            foreach (var role in roleList)
            {
                var rolePermissions = role.T_Permissions.Select(u => new PerissionDTO()
                {
                    Id = u.Id,  
                    Description=u.Description,
                    Name = u.Name
                });
                perissionList.AddRange(rolePermissions);    
            }
            return perissionList;   
        }
    }
}
